package com.ccl.iot.device;


import java.util.ArrayList;
import java.util.Arrays;

public class SingleProbe{
	
	private byte[] mReg = new byte[4];
	private int mNewTriggerCount = 0;
	
	private int mAddress = 0;
	
	private ArrayList<String> mTimeData = new ArrayList<String>();
	private ArrayList<Long> mRegLogs = new ArrayList<Long>();

	public SingleProbe(byte[] b, int address) {
		if(b != null){
			for (int i = 0; i < b.length; i++) {
				mReg[i] = b[i];
			}
		}
		
		mAddress = address;
		
	}

	public void setRegDatas(byte[] newData) {
		if(newData != null){
			int before = getTriggerCount();
			for (int i = 0; i < newData.length; i++) {
				mReg[i] = newData[i];
			}

			int after = getTriggerCount();
			mNewTriggerCount += after - before;
			if(after - before > 0 && mListener != null){
//				ThreadPoolUtils.getInstance().addTask(new Runnable() {
//
//					@Override
//					public void run() {
						mListener.onTirggerCountChanged(SingleProbe.this);
//					}
//				});
			}
		}
	}
	
	public byte[] getRegDatas(){
		return Arrays.copyOfRange(mReg, 0, mReg.length);
	}
	
	public int getNewTriggerCount(){
		return mNewTriggerCount;
	}
	
	public void clearNewTriggerCount(){
		mNewTriggerCount = 0;
	}
	
	public int getTriggerCount(){
		return ((0xffffffff & mReg[3]) << 8) | (0xff & mReg[2]);
	}
	
	public void addTimeData(String time, long refid) {
		mTimeData.add(0, time);
		mRegLogs.add(0, refid);
	}
	
	public void addTimeDataEnd(String time, long refid){
		mTimeData.add(time);
		mRegLogs.add(refid);
	}
	
	public void removeTimeData(int index){
		mTimeData.remove(index);
		mRegLogs.remove(index);
	}
	
	public void cleanTimeData(){
		mTimeData.clear();
		mRegLogs.clear();
	}
	
	public int getTimeDataCount(){
		return mTimeData.size();
	}
	
	public long getRefId(){
		if(mRegLogs.size() > 0){
			return mRegLogs.get(0);
		}
		return 0;
	}
	
	public int getAddress(){
		return mAddress;
	}
	
	public ArrayList<String> getTimeData(){
		return new ArrayList<String>(mTimeData);
	}
	
	public interface OnTirggerCountChangedListener{
		public void onTirggerCountChanged(SingleProbe probe);
	}
	
	private OnTirggerCountChangedListener mListener;
	public void setListener(OnTirggerCountChangedListener l){
		mListener = l;
	}
	
}
